package com.zaofan.utils;

import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.zaofan.bean.Setting;
import com.zaofan.service.GoodsOrderInfoService;
import com.zaofan.service.OrderInfoService;
import com.zaofan.service.SettingService;
import com.zaofan.service.remote.RemoteService;

public class TaskOrderContextListner implements ServletContextListener {
	private static java.util.Timer timer = null;
	private OrderInfoService orderService = new OrderInfoService();
	private GoodsOrderInfoService goodsOrderInfoService = new GoodsOrderInfoService();
	private SettingService settingService = new SettingService();
	private RemoteService remoteService = new RemoteService();

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		if (timer != null)
			timer.cancel();
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		timer = new java.util.Timer(true);
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				System.err.println("执行-----------------订单过时处理-------------------任务");
				Setting setting = settingService.findSetting();
				System.err.println("执行-----------------异步处理订单超时优惠券返回-------------------任务");
				remoteService.backCouponTimeout(setting.getOrderTimeOut() * 60);
				//条件 未超时的且条件都符合的

				//在这之后再处理订单设为超时操作v

				orderService.updateWithOutPay(setting.getOrderTimeOut() * 60);
				goodsOrderInfoService.updateWithOutPay(setting.getGoodsOrderTimeOut() * 60);


				System.err.println("任务-----------------订单过时处理-------------------结束");
			}
		}, 0, 1 * 60 * 1000);
	}

}
